#include "Rock.h"
#include "Global.h"
USING_NS_CC;

Rock* Rock::create(cocos2d::Layer* layer, float posX)
{
	Rock* pRet = new Rock();

	if(pRet && pRet->init(layer, posX))
	{
		pRet->autorelease();
		return pRet;
	}
	else
	{
		delete pRet;
		pRet = NULL;
		return NULL;
	}
}

bool Rock::init(cocos2d::Layer* layer, float posX)
{
	if(!Node::init())
	{
		return false;
	}

	sprite = Sprite::create("rock.png");
	sprite->setTag(TagOfSprite::ROCK_SPRITE);

	auto contentSize = sprite->getContentSize();

	sprite->setPosition(posX, contentSize.height/2 + Global::g_groundHeight);

	auto material = PHYSICSBODY_MATERIAL_DEFAULT;
	material.density = 1.0f;
	material.friction = 0.0f;
	material.restitution = 0.0f;

	auto body = PhysicsBody::createBox(contentSize, material);
	body->setGravityEnable(false);
	body->setContactTestBitmask(true);
	body->setDynamic(false);

	sprite->setPhysicsBody(body);

	this->addChild(sprite, 1);

	return true;
}